package com.hzj.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hzj.controller.utils.IPUtils;
import com.hzj.controller.utils.RedisUtil;
import com.hzj.exception.HospitalException;
import com.hzj.pojo.UserInfo;
import com.hzj.pojo.vo.user.LoginVo;
import com.hzj.pojo.vo.user.UserAuthVo;
import com.hzj.service.UserInfoService;
import com.hzj.utils.Result;
import com.hzj.utils.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/user/")
public class ApiUserController {
    @Autowired
    UserInfoService uis;
    @Autowired
    RedisUtil redisUtil;

    @PostMapping("/login")
    Map<String,Object> loginUser(@RequestBody LoginVo loginVo, HttpServletRequest request) {
        System.out.println("传过来的值" + loginVo);
        Map<String, Object> map = uis.login(loginVo, request);
        return map;
    }

    @PostMapping("/auth/getUserInfo")
    Result getUserInfo(HttpServletRequest request) {
        UserInfo userInfo = (UserInfo)redisUtil.get("userInfo"+ IPUtils.getIpAddr(request));
//        System.out.println("UserInfo:" + userInfo+"|ip:"+IPUtils.getIpAddr(request));
        if (userInfo==null){
            return Result.build(201,"未登录");
        }
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", userInfo.getPhone());
        UserInfo one = uis.getOne(wrapper);
        return Result.ok(one);
    }

    @PostMapping("/auth/userAuah")
    Result userAuah(@RequestBody UserAuthVo userAuthVo,HttpServletRequest request) {
        UserInfo userInfo = (UserInfo)redisUtil.get("userInfo"+ IPUtils.getIpAddr(request));
        System.out.println("实名认证sessionid:"+request.getSession().getId());
//        String phone = userInfo.getPhone();
        UserInfo userInfo1 = new UserInfo();
        userInfo1.setName(userAuthVo.getName());
        userInfo1.setCertificatesNo(userAuthVo.getCertificatesNo());
        userInfo1.setCertificatesType(userAuthVo.getCertificatesType());
        userInfo1.setCertificatesUrl(userAuthVo.getCertificatesUrl());
        userInfo1.setStatus(1);
        userInfo1.setAuthStatus(1);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("phone", userInfo.getPhone());//把固定值换成phone

        uis.saveOrUpdate(userInfo1, wrapper);


        return Result.ok();
    }

}
